package com.ljxt.dt.web.servlet.storemanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.ReceptType;
import com.ljxt.dt.service.storemanagementservice.DetailService;
import com.ljxt.dt.service.storemanagementservice.EditService;
import com.ljxt.dt.service.storemanagementservice.impl.DetailServiceImpl;
import com.ljxt.dt.service.storemanagementservice.impl.EditServiceImpl;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.HandlerInfoVO;
import com.ljxt.dt.vo.storemanagement.ReceptDetailVO;
import com.ljxt.dt.vo.storemanagement.ReceptVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-12 16:27
 * @copyright 老九学堂
 */
@WebServlet("/edit")
public class Edit extends HttpServlet {
    private EditService admin = new EditServiceImpl();
    private DetailService detailService = new DetailServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id =  req.getParameter("receptNo");

        ReceptVO receptInfo =  detailService.selectDetail(id);
        //查询可修改的入库类型
            List<ReceptType> list =  admin.selectType();


        List<ReceptDetailVO> receptDetailVO = detailService.selectDetailGoods(id);
        //创建一个pageVO
        PageVO pageVO = new PageVO(receptDetailVO,-1,-1,-1);
        //将它转为js对象
        ObjectMapper objectMapper = new ObjectMapper();
        req.setAttribute("pageVO",objectMapper.writeValueAsString(pageVO));

        req.setAttribute("vo",receptInfo);
        req.setAttribute("type",list);

        //获取经办信息
        List<HandlerInfoVO> handlerInfoVOS = detailService.selectInfo(id);
        //创建一个pageVO
        PageVO<HandlerInfoVO> vo = new PageVO<>(handlerInfoVOS,-1,-1,-1);
        req.setAttribute("info",objectMapper.writeValueAsString(vo));
        ServletUtil.forward(req,resp,"edit","storeManagement");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("receptNo");
        //查询是否是待提交状态
        boolean isTrue =  admin.isEdit(id);
        //查询到所有
        resp.getWriter().print(isTrue);

    }
}
